﻿using Epm.AppLogic.Framework;
using msBsc.Xsd.DataTds;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Xml;
using System.Xml.Linq;
using System.Linq;
using System.Web.Configuration;

namespace msBsc.AppLogic.DataAlc
{
    public class CBizDjkRouteBaseinfo : CustBizObject
    {
        private CDmDjkRouteBaseinfo m_CDmDjkRouteBaseinfo = new CDmDjkRouteBaseinfo();
        private CDmDjkRouteBaseinfo oDmDjkRouteBaseinfo
        {
            get { return m_CDmDjkRouteBaseinfo; }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public CBizDjkRouteBaseinfo()
            : base()
        {
            this.DataObj = new CDbAccess();
            this.DataObj.Init(this.m_CDmDjkRouteBaseinfo.DJK_ROUTE_BASEINFO);
            this.TableName = this.DataObj.TableName;
            this.DataObj.KeyField = this.DataObj.Fields["ROUTE_BASEINFO_ID"];
        }

        /// <summary>
        /// 新增记录前的处理
        /// </summary>
        /// <param name="drw">新增数据行</param>
        /// <returns>true/false</returns>
        /// <remarks>
        /// 生成ID值
        /// </remarks>
        protected override bool BeforeRowAdded(DataRow drw)
        {
            if (drw[this.KeyFieldName].ToString().Length < 10)
            {
                drw[this.KeyFieldName] = this.DataObj.NewID();
            }
            return true;
        }

        /// <summary>
        /// 记录修改前检验
        /// </summary>
        /// <param name="drw">当前记录</param>
        /// <returns>>校验合法，返回值为true,否则返回false</returns>
        /// <remarks>
        /// ID不允许修改,并把此修改操作记录到BSE_MODIFY_HISTORY表中
        /// </remarks>
        protected override bool BeforeRowModified(DataRow drw)
        {
            //ID不容许修改
            if (drw[this.KeyFieldName].ToString() != drw[this.KeyFieldName, DataRowVersion.Original].ToString())
            {
                Exception e = new Exception(this.TableName + "表的主键" + this.KeyFieldName + "字段不允许被修改");
                throw e;
            }
            return true;
        }
        protected override bool BeforeRowDeletedR(DataRow drw)
        {
            //using (msBsc.AppLogic.DataAlc.CBizShpPageColumn objtem = new CBizShpPageColumn())
            //{
            //    DataSet ds = objtem.SelectByPageNo(this.GetRowStr(drw, KeyFieldName));
            //    for (int i = ds.Tables[0].Rows.Count - 1; i >= 0; i--)
            //    {
            //        ds.Tables[0].Rows[i].Delete();
            //    }
            //    objtem.Update(ds);
            //} 
            return true;
        }

        protected override bool AfterRowAdded(DataRow drw)
        {
            return true;
        }

        protected override bool AfterRowModified(DataRow drw)
        {
            return true;
        }

        protected override bool AfterRowDeletedR(DataRow drw)
        {
            return true;
        }

        /// <summary>
        /// 添加 路线推荐
        /// </summary>
        /// <param name="title">路线名称</param>
        /// <param name="introduction">路线简介</param>
        /// <param name="numberDays">路线天数</param>
        /// <param name="country">国家</param>
        /// <param name="province">省</param>
        /// <param name="area">区</param>
        /// <param name="countryName">国家名称</param>
        /// <param name="provinceName">省名称</param>
        /// <param name="areaName">区名称</param>
        /// <param name="scenicSpots">主要景点</param>
        /// <param name="imageExplain">路线首图</param>
        /// <param name="routeCountent">预定说明</param>
        /// <param name="price">价格</param>
        /// <param name="ispush">设为推荐</param>
        /// <returns>string</returns>
        /// Author:蒲崇彬
        /// Creation date:2015-8-3 12:05
        public string[] AddRoute(string title, string introduction, int numberDays, string country, string province, string area, string countryName, string provinceName, string areaName, string scenicSpots, string imageExplain, string routeCountent, string price, int ispush, string createID)
        {

            string routeid = this.DataObj.NewID();
            DataSet dsRoute = new CDmDjkRouteBaseinfo();
            DataRow drRoute = dsRoute.Tables[0].NewRow();
            drRoute["ROUTE_BASEINFO_ID"] = routeid;
            drRoute["ROUTE_BASEINFO_NME"] = title;
            drRoute["ROUTE_BASEINFO_INTRO"] = introduction;


            drRoute["COUNTRY_ID"] = country;
            drRoute["CITY_ID"] = province;
            drRoute["DESTINATION_ID"] = area;

            drRoute["COUNTRY_NME"] = countryName;
            drRoute["CITY_NME"] = provinceName;
            drRoute["DESTINATION_NME"] = areaName;
            //主要景点
            drRoute["ROUTE_MAIN_SCENICS"] = scenicSpots;
            //首图
            drRoute["ROUTE_DEFAULT_PIC"] = imageExplain;
            //价格
            drRoute["ROUTE_DEFAULT_PRICE"] = price;

            //是否为推荐线路
            drRoute["ROUTE_IS_PUSH"] = ispush;
            //日程天数
            drRoute["ROUTE_BASEINFO_DAYS"] = numberDays;
            //是否显示
            drRoute["ROUTE_IS_SHOW"] = 1;
            //路线状态
            drRoute["ROUTE_INFO_STATUS"] = 1;
            //创建人
            drRoute["CREATE_ID"] = createID;
            drRoute["CREATE_DT"] = this.dtCurrDateTime;
            //预定说明
            drRoute["ROUTE_ORDER_EXPIRE"] = routeCountent;

            dsRoute.Tables[0].Rows.Add(drRoute);
            dsRoute.Tables[0].TableName = this.TableName;
            this.Update(dsRoute);
            return new string[] { "true", routeid };
        }
        /// <summary>
        /// 修改 路线推荐
        /// </summary>
        /// <param name="routeid">要修改的路线id</param>
        /// <param name="title">路线名称</param>
        /// <param name="introduction">路线简介</param>
        /// <param name="numberDays">路线天数</param>
        /// <param name="country">国家</param>
        /// <param name="province">省</param>
        /// <param name="area">区</param>
        /// <param name="countryName">国家名称</param>
        /// <param name="provinceName">省名称</param>
        /// <param name="areaName">区名称</param>
        /// <param name="scenicSpots">主要景点</param>
        /// <param name="imageExplain">路线首图</param>
        /// <param name="routeCountent">预定说明</param>
        /// <param name="price">价格</param>
        /// <param name="ispush">设为推荐</param>
        /// <returns>string</returns>
        /// Author:蒲崇彬
        /// Creation date:2015-8-3 12:05
        public string[] UpdateRoute(string routeid, string title, string introduction, int numberDays, string country, string province, string area, string countryName, string provinceName, string areaName, string scenicSpots, string imageExplain, string routeCountent, string price, int ispush, string updateUserid)
        {
            if (title.Length > 30)
            {
                return new string[] { "false", "标题应该在30字以内" };
            }
            if (introduction.Length > 100)
            {
                return new string[] { "false", "简介应该在100字以内" };
            }

            DataSet ds = GetRouteInfoByID(routeid);
            DataTable dt = ds.Tables[0];
            if (dt == null)
            {
                return new string[] { "false", "未找到该路线信息" };
            }
            DataRow drRoute = dt.Rows[0];
            drRoute["ROUTE_BASEINFO_ID"] = routeid;
            drRoute["ROUTE_BASEINFO_NME"] = title;
            drRoute["ROUTE_BASEINFO_INTRO"] = introduction;


            drRoute["COUNTRY_ID"] = country;
            drRoute["CITY_ID"] = province;
            drRoute["DESTINATION_ID"] = area;

            drRoute["COUNTRY_NME"] = countryName;
            drRoute["CITY_NME"] = provinceName;
            drRoute["DESTINATION_NME"] = areaName;
            //主要景点
            drRoute["ROUTE_MAIN_SCENICS"] = scenicSpots;
            //首图
            drRoute["ROUTE_DEFAULT_PIC"] = imageExplain;
            //价格
            drRoute["ROUTE_DEFAULT_PRICE"] = price;

            //是否为推荐线路
            drRoute["ROUTE_IS_PUSH"] = ispush;
            //日程天数
            drRoute["ROUTE_BASEINFO_DAYS"] = numberDays;
            //是否显示
            drRoute["ROUTE_IS_SHOW"] = 1;
            //路线状态
            drRoute["ROUTE_INFO_STATUS"] = 1;
            //修改人
            drRoute["MODIFY_ID"] = updateUserid;
            drRoute["MODIFY_DT"] = this.dtCurrDateTime;
            //预定说明
            drRoute["ROUTE_ORDER_EXPIRE"] = routeCountent;

            this.Update(ds);
            return new string[] { "true", routeid };
        }
        /// <summary>
        /// 获取路线信息
        /// </summary>
        /// <param name="routeID">路线ID</param>
        /// <returns>DataTable</returns>
        /// Author:蒲崇彬
        /// Creation date:2015-9-22
        public DataSet GetRouteInfoByID(string routeID)
        {
            if (string.IsNullOrEmpty(routeID))
            {
                return null;
            }

            CDbParmCollection parmCollection = new CDbParmCollection();
            parmCollection.Add(new CDbParm("routeID", ParameterDirection.Input, EDbType.String, 36, routeID));

            DataSet ds = this.DataObj.QueryBySQL(@"SELECT * FROM DJK_ROUTE_BASEINFO WHERE ROUTE_BASEINFO_ID=@routeID", parmCollection);
            if (ds == null || ds.Tables[0].Rows.Count < 1)
            {
                return null;
            }
            ds.Tables[0].TableName = this.TableName;
            return ds;
        }

        /// <summary>
        /// 修改是否显示 状态
        /// </summary>
        /// <param name="routeid">路线id</param>
        /// <param name="userid">用户id</param>
        /// <param name="status">状态 0隐藏 1显示</param>
        /// <returns></returns>
        /// Author:蒲崇彬
        /// Creation date:2015-9-25
        public string UpdateStatus(string routeid, string userid, string status)
        {
            DataSet ds = GetRouteInfoByID(routeid);
            if (ds == null || ds.Tables[0].Rows.Count < 1)
            {
                return "该路线不存在";
            }

            ds.Tables[0].Rows[0]["ROUTE_IS_SHOW"] = status;
            //修改人
            ds.Tables[0].Rows[0]["MODIFY_ID"] = userid;
            ds.Tables[0].Rows[0]["MODIFY_DT"] = this.dtCurrDateTime;
            this.Update(ds);

            return "OK";
        }

        /// <summary>
        /// 获取推荐路线
        /// </summary>
        /// <param name="countryId">国家</param>
        /// <param name="cityId">城市</param>
        /// <param name="destionId">地区</param>
        /// <param name="rows"></param>
        /// <returns></returns>
        /// Author:蒲崇彬
        /// Creation date:2015-9-25
        public DataSet GetRecommendRoute(string countryId, string cityId, string destionId, int day, int rows = 2)
        {
            if (string.IsNullOrEmpty(countryId))
            {
                return null;
            }
            string queryStr = "";
            CDbParmCollection parmCollection = new CDbParmCollection();

            parmCollection.Add(new CDbParm("countryId", ParameterDirection.Input, EDbType.String, 36, countryId));
            parmCollection.Add(new CDbParm("day", ParameterDirection.Input, EDbType.Integer, 36, day));
            if (!string.IsNullOrEmpty(cityId))
            {
                queryStr += " AND  CITY_ID=@cityId ";
                parmCollection.Add(new CDbParm("cityId", ParameterDirection.Input, EDbType.String, 36, cityId));
            }
            if (!string.IsNullOrEmpty(destionId))
            {
                queryStr += " AND  DESTINATION_ID=@destionId ";
                parmCollection.Add(new CDbParm("destionId", ParameterDirection.Input, EDbType.String, 36, destionId));
            }

            return this.DataObj.QueryBySQL(@"SELECT TOP " + rows + " * FROM DJK_ROUTE_BASEINFO WHERE ROUTE_IS_PUSH=1 AND ROUTE_BASEINFO_DAYS=@day  AND COUNTRY_ID=@countryId " + queryStr + " ORDER BY MODIFY_DT DESC,CREATE_DT DESC", parmCollection);
        }
        /// <summary>
        /// 获取推荐地区 分组
        /// </summary>
        /// <param name="countryId"></param>
        /// <param name="cityId"></param>
        /// <param name="destionId"></param>
        /// <returns></returns>
        /// Author:蒲崇彬
        /// Creation date:2015-9-25
        public DataSet GetRecommendDay(string countryId, string cityId, string destionId)
        {
            if (string.IsNullOrEmpty(countryId))
            {
                return null;
            }
            string queryStr = "";
            CDbParmCollection parmCollection = new CDbParmCollection();

            parmCollection.Add(new CDbParm("countryId", ParameterDirection.Input, EDbType.String, 36, countryId));
            if (!string.IsNullOrEmpty(cityId))
            {
                queryStr += " AND  CITY_ID=@cityId ";
                parmCollection.Add(new CDbParm("cityId", ParameterDirection.Input, EDbType.String, 36, cityId));
            }
            if (!string.IsNullOrEmpty(destionId))
            {
                queryStr += " AND  DESTINATION_ID=@destionId ";
                parmCollection.Add(new CDbParm("destionId", ParameterDirection.Input, EDbType.String, 36, destionId));
            }
            //获取该地区的分组，2.3.5.8 天的都不一定，找到最多的
            return this.DataObj.QueryBySQL(@"SELECT ROUTE_BASEINFO_DAYS,COUNT(1) CT FROM DJK_ROUTE_BASEINFO WHERE ROUTE_IS_PUSH=1 " + queryStr + @" GROUP BY ROUTE_BASEINFO_DAYS  ORDER BY CT DESC");
        }
        /// <summary>
        /// 获取地区下的 所有路线
        /// </summary>
        /// <param name="destionId">地区</param>
        /// <param name="pageSize">每页数量</param>
        /// <param name="pageIndex">第几页</param>
        /// <returns></returns>
        /// Author:蒲崇彬
        /// Creation date:2015-9-25
        public DataSet GetDestionRouts(string destionId, int pageSize, int pageIndex)
        {
            CDbParmCollection parmCollection = new CDbParmCollection();
            parmCollection.Add(new CDbParm("destionId", ParameterDirection.Input, EDbType.String, 36, destionId));
            parmCollection.Add(new CDbParm("pagesize", ParameterDirection.Input, EDbType.String, 36, pageSize));
            parmCollection.Add(new CDbParm("pageindex", ParameterDirection.Input, EDbType.String, 36, pageIndex));
            return this.DataObj.QueryBySP("DJK_ROUTE_BASEINFO_LIST_VIEW", parmCollection);
        }
    }
}
